# -*- coding: utf-8 -*-
# Create your views here.
from google.appengine.api import urlfetch
import urllib
import time
import datetime
from models import NewsItem
from django.http import HttpResponse
from django.shortcuts import render_to_response
from google.appengine.api import memcache
import logging

def getStrBetween(string, start, end, index):
	if len(start) == 0:
		a = index
	else:
		a = string.find(start, index)
	if a == -1:
		return '', -1
	a = a + len(start)
	b = string.find(end, a)
	if b == -1:
		return '', -1
	return string[a:b], b+len(end)

def handle_OAdetail(rpc, url):
	domain = 'http://buptoa.bupt.edu.cn/'
	
	if 'student_broad.nsf' in url:
		site = u'学生布告栏'
	elif 'broad.nsf' in url:
		site = u'办公布告栏'

	result = rpc.get_result()
	content = result.content
	html =  content.decode('gb18030')

	start = html.find('<B><FONT SIZE=2>')
	start = html.find('<B><FONT SIZE=2>', start + len('<B><FONT SIZE=2>'))
	start = html.find('<B><FONT SIZE=2>', start + len('<B><FONT SIZE=2>'))
	[title, start] = getStrBetween(html, '<B><FONT SIZE=2>', '</FONT>', start)
	title = title.replace('<BR>', ' ')

	start = html.find('</TABLE>', start)
	start = html.find('</TABLE>', start + len('</TABLE>'))
	end  = html.find('<INPUT')
	detail = html[start+len('</TABLE>'): end]
	detail = detail.replace('SRC="/', u'SRC="' + domain )
	detail = detail.replace('HREF="/', u'HREF="' + domain )

	index = end-1
	[publisher, index] = getStrBetween(html, '<INPUT NAME="From" TYPE=hidden VALUE="', '">', index)
	[day, index] = getStrBetween(html, '<INPUT NAME="DateComposed" TYPE=hidden VALUE="', '">', index)
	[time, index] = getStrBetween(html, '<INPUT NAME="timeComposed" TYPE=hidden VALUE="', '">', index)
	[category, index] = getStrBetween(html, '<INPUT NAME="class" TYPE=hidden VALUE="', '">', index)
	date = datetime.datetime.strptime(str(datetime.datetime.today().year) + '-' +day+' '+time, '%Y-%m-%d %H:%M')

	newsItem = NewsItem(title=title, 
			publisher=publisher, 
			description=detail, 
			link=url,
			date=date,
			category=category,
			site=site)
	newsItem.put()

def handle_Homedetail(rpc, url):
	domain = 'http://www.bupt.edu.cn/'
	site = u'主页新闻'
	
	result = rpc.get_result()
	content = result.content
	html =  content.decode('utf-8')

	index = 0
	[title, index] = getStrBetween(html, u'<ul id="act_title">', u'</ul>', index)
	[day, index] = getStrBetween(html, u'<ul id="act_inf">发布时间:', u'&nbsp;&nbsp;', index)
	[publisher, index] = getStrBetween(html, u'出处:', u'&nbsp;&nbsp;', index)
	[detail, index] = getStrBetween(html, u'<ul id="act_main">', u'<div id="footer">', index)
	detail = detail.replace(u'src="/', u'src="' + domain )
	category = u'新闻'
	#date = datetime.datetime.strptime(day, '%Y-%m-%d')
	date = datetime.datetime.now()

	newsItem = NewsItem(title=title, 
			publisher=publisher, 
			description=detail, 
			link=url,
			date=date,
			category=category,
			site=site)
	newsItem.put()


def create_callback(rpc, url):
	if 'http://buptoa.bupt.edu.cn/' in url:
		return lambda: handle_OAdetail(rpc, url)
	elif 'http://www.bupt.edu.cn' in url:
		return lambda: handle_Homedetail(rpc, url)

def getNewsDetail(links):
	rpcs = []
	for link in links:
		rpc = urlfetch.create_rpc(deadline=10)
		rpc.callback = create_callback(rpc, link)
		urlfetch.make_fetch_call(rpc, link)
		rpcs.append(rpc)
	
	for rpc in rpcs:
		rpc.wait()


def getBuptpaNews(link):
	domain = 'http://buptoa.bupt.edu.cn/'
	url = domain + link
	response = urlfetch.fetch(url, deadline=10)
	content = response.content
	html = content.decode('gb18030', 'ignore')

	index = 0
	links = []
	while( index != -1 and index < len(html)):
		[link, index] = getStrBetween(html, u'<FONT SIZE=2 COLOR="008080"><A HREF="', u'">', index)
		if index == -1:
			break
		link = domain[:-1] + link
		re = NewsItem.all().filter('link = ', link)
		if re.count() > 0:
			break
		links.append(link)
	getNewsDetail(links)

def getHomeNews():
	domain = 'http://www.bupt.edu.cn/'
	url = domain
	response = urlfetch.fetch(url, deadline=10)
	content = response.content
	html = content.decode('utf-8', 'ignore')

	index = 0
	links = []
	while( index != -1 and index < len(html)):
		[title, index] = getStrBetween(html, u'<li class="news_list"><a title="', u'"', index)
		if index == -1:
			break
		[link, index] = getStrBetween(html, u'href="', u'"', index)
		link = domain[:-1] + link
		re = NewsItem.all().filter('link = ', link)
		if re.count() > 0:
			break
		links.append(link)
	getNewsDetail(links)

def updateMemcache():
	all_items = NewsItem.all().order('-date').fetch(20)
	office_items = NewsItem.all().filter('site = ', u'办公布告栏').order('-date').fetch(20)
	students_items = NewsItem.all().filter('site = ', u'学生布告栏').order('-date').fetch(20)
	home_items = NewsItem.all().filter('site = ', u'主页新闻').order('-date').fetch(20)
	fail_list= memcache.set_multi({ "all_items": all_items,
		"office_items": office_items,
		"students_items": students_items,
		"home_items": home_items}
		)

	if len(fail_list) > 0:
		logging.error('Memcache set error!')


def getNewsItem(request):
	urls = ['student_broad.nsf/($All)?OpenView', 'broad.nsf/($All)?OpenView']
	for url in urls:
		getBuptpaNews(url)
	
	getHomeNews()
	updateMemcache()
	return HttpResponse('Update News successed!')

def deletMonthBefore(request):
	deadline = datetime.date.today()
	deadline -= datetime.timedelta(days=30)
	res = NewsItem.all().filter('date < ', deadline)
	num = 0
	for re in res:
		re.delete()
		num += 1
	return HttpResponse('Delete %d items!' %num)

def mainview(request):
	return render_to_response('main.html')
